package robotinterface;

import dialogs.Export;
import java.awt.event.*;
import javax.swing.*;
import robotinterface.list.*;

/**
 *
 * Nate Hemmes / Brian Bailey
 */
public class SaveButtonPanel extends JPanel{

    private RobotList rList;
    JToggleButton runButton;
    
    public SaveButtonPanel(RobotList rList)
    {
        this.rList = rList;
        
        SpringLayout saveButtonLayout = new SpringLayout();
        setLayout(saveButtonLayout);
        setBorder(BorderFactory.createEtchedBorder());

        JButton exportButton = new JButton("Export");
        exportButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ab) {
                try {
                    Export dialog = new Export();
                    dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
                    dialog.setVisible(true);
                } 
                catch (Exception exp) { exp.printStackTrace(); }
            }
        });
        add(exportButton);
        saveButtonLayout.putConstraint(SpringLayout.WEST, exportButton, 5, SpringLayout.WEST, this);
        saveButtonLayout.putConstraint(SpringLayout.NORTH, exportButton, 5, SpringLayout.NORTH, this);
        saveButtonLayout.putConstraint(SpringLayout.SOUTH, exportButton, -5, SpringLayout.SOUTH, this);
        saveButtonLayout.putConstraint(SpringLayout.EAST, exportButton, 101, SpringLayout.WEST, this);
        exportButton.setToolTipText( "Export list to macro." );

        runButton = new JToggleButton("Run");
        runButton.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent ae) {
                try {
                    //Run dialog = new Run();
                    //dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
                    //dialog.setVisible(true);
                    runList();
                } catch (Exception exp) { exp.printStackTrace(); }
            }
        });         
        runButton.addItemListener(new ItemListener(){
            @Override
            public void itemStateChanged( ItemEvent ev )
            {
                JToggleButton button = (JToggleButton)ev.getSource();
                if (ev.getStateChange()==ItemEvent.SELECTED) { button.setText("Stop"); } 
                else { button.setText( "Run" ); }
            }
        });
        
        add(runButton);
        saveButtonLayout.putConstraint(SpringLayout.WEST, runButton, 5, SpringLayout.EAST, exportButton);
        saveButtonLayout.putConstraint(SpringLayout.NORTH, runButton, 5, SpringLayout.NORTH, this);
        saveButtonLayout.putConstraint(SpringLayout.SOUTH, runButton, -5, SpringLayout.SOUTH, this);
        saveButtonLayout.putConstraint(SpringLayout.EAST, runButton, -5, SpringLayout.EAST, this);
        runButton.setToolTipText( "Run current list." );
    }
        
    private void runList( )
    {
        if (runButton.isSelected()) { rList.start(); }
        else { rList.stop(); }
    }
}
